package com.molyfun.parents.activity.record.itemview;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.RecordResponse;
import com.molyfun.parents.service.MediaUtils;
import com.molyfun.parents.service.music.MusicStatusCenter;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/3/16.
 *
 * 此类为容器
 */
public class RecordVoiceView extends BaseRecordView implements MusicStatusCenter.IMusicStatus {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.record_user)
    TextView mRecordUser;
    @InjectView(R.id.time_sec)
    TextView mTimeSec;
    @InjectView(R.id.voice_bar_layout)
    FrameLayout mVoiceBarLayout;
    @InjectView(R.id.voice_animation)
    ImageView mVoiceIcon;
    private AnimationDrawable animationDrawable;

    public RecordVoiceView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public RecordVoiceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RecordVoiceView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_record_voice, this);
        ButterKnife.inject(this);
        mVoiceIcon.setImageResource(R.drawable.voice_play_animation);
        //给动画资源赋值
        animationDrawable = (AnimationDrawable) mVoiceIcon.getDrawable();
        animationDrawable.stop();
    }

    /**
     * init view
     *
     */
    public void initData(RecordResponse.RecordBean recordBean) {
        if(recordBean != null){
            mRecordBean = recordBean;
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams)mVoiceBarLayout.getLayoutParams();
            layoutParams.width = 140 + (int)((recordBean.getTimelen() / 60.0) * (MFPApplication.sScreenWidth >> 1 ) );
            mVoiceBarLayout.setLayoutParams(layoutParams);
            if(recordBean.getTimelen() <= 0){
                mTimeSec.setVisibility(View.GONE);
            }else{
                mTimeSec.setVisibility(View.VISIBLE);
                mTimeSec.setText(recordBean.getTimelen() + " \"");
            }
            mRecordUser.setText(recordBean.getUsername());
            mVoiceBarLayout.setOnClickListener(this);
            mVoiceBarLayout.setOnLongClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.voice_bar_layout:
                MusicStatusCenter.registerMusicStatusListener(this);
                System.out.println("onClick--->isCurrentPlayingAudio" + MediaUtils.isCurrentPlayingAudio());
//        if(MediaUtils.isCurrentPlayingAudio()){
//            MediaUtils.stopPlayAudio(context);
//        }
                MediaUtils.startPlayAudio(context, mRecordBean.getUrl());
                break;
        }
    }

    @Override
    public void onMusicPause(String musicItem) {
        animationDrawable.stop();
    }

    @Override
    public void onMusicPlay(String musicItem) {
        System.out.println("onMusicPlay--->" + musicItem);
        if(!musicItem.equals(mRecordBean.getUrl())){
            animationDrawable.stop();
            mVoiceIcon.setImageResource(R.mipmap.ic_voice_ani3);
        }else{
            mVoiceIcon.setImageResource(R.drawable.voice_play_animation);
            //给动画资源赋值
            animationDrawable = (AnimationDrawable) mVoiceIcon.getDrawable();
            animationDrawable.start();
        }
    }

    @Override
    public void onMusicStop(String musicItem) {
        System.out.println("onMusicStop--->" + musicItem);
        if(musicItem.equals(mRecordBean.getUrl())){
            animationDrawable.stop();
            mVoiceIcon.setImageResource(R.mipmap.ic_voice_ani3);
        }
    }
}